from django.http import HttpResponseRedirect
from models import User
import logging

def auth_check(view_func): 
    def _wrapped_view_func(request, *args, **kwargs): 
        if ('pbusr' not in request.session and 'pbusradmn' not in request.session):
            return HttpResponseRedirect("login") 
        return view_func(request, *args, **kwargs) 
    return _wrapped_view_func

def auth_check_is_admin(view_func): 
    def _wrapped_view_func(request, *args, **kwargs):
        if ('pbusr' in request.session):
            logging.info("ciao")
            u = User.get(request.session["pbusr"])
            if(u is not None and u.isAdmin):
                return view_func(request, *args, **kwargs) 
        return HttpResponseRedirect("/")
    return _wrapped_view_func

